var delObj = {tip:'任务相关配置会一并删除,确认删除任务吗?',url:"deleteTask"};
var resetObjs = [{id:"taskName",value:""},{id:"taskGroupName",value:0}];
$(function(){
	var url = window.location.href.split("=");
	var groupId = 0;
	if(url.length == 2){
		groupId = url[1].replace("#","");
	}
	search(1,groupId);
	listGatherTaskGroup(groupId);
})

function search(){
	var currPage = arguments[0];
	var groupId = $("#taskGroupName").val();
	if(arguments.length == 2){
		groupId = arguments[1];
	}
	$.ajax({
		type : "post",
		url : "taskPageList",
		data : {
			"taskName":$.trim($("#taskName").val()),
			"taskGroupId":groupId,
			"start":currPage
		},
		dataType : "json",
		success : function(data) {
			if(data.msg == 'success'){
				var html = '';
				$.each(data.datas,function(k,v){
					html += '<tr><td><input type="checkbox" name="check" value='+v.id+' /></td>'
								+'<td>'+(k+1)+'</td>'
								+'<td><label>'+v.name+'</label></td>'
								+'<td><label>'+v.gtg_name+'</label></td>'
								+'<td><label>'+v.createTime+'</label></td>'
								+'<td name="'+v.name+'" summary="'+v.summary+'" id="'+v.id+'" groupId="'+v.taskGroupId+'">'
								+'<button type="button" class="btn btn-blue btn-xs" onclick="toConfigHtml(\''+v.id+'\');">任务配置</button>&nbsp;&nbsp;&nbsp;'
								+'<button type="button" class="btn btn-yellow btn-xs" onclick="addOrEdit(this);">编辑</button>&nbsp;&nbsp;&nbsp;'	
								+'<button type="button" class="btn btn-red btn-xs" onclick="delOne(\''+v.id+'\');">删除</button>&nbsp;&nbsp;&nbsp;'
								+'</td></tr>';
				});
				$("#datas").html(html);
				laypage({
					cont : document.getElementById('page1'), //容器。值支持id名、原生dom对象，jquery对象,
					pages : data.pages, //总页数
					skip : true, //是否开启跳页
					skin : 'molv', //加载内置皮肤，也可以直接赋值16进制颜色值，如:#c00
					curr : currPage,
					last: data.pages,
					groups : 6,//连续显示分页数
					jump : function(obj, first) { //触发分页后的回调
						if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
							search(obj.curr,groupId);
						}
					}
				});
			}else{
				layer.msg("获取数据失败");
			}
			
		},
		error : function(data) {
			layer.msg("获取数据失败");
		}
	});
}

function listGatherTaskGroup(groupId){
	$.post("listTaskGroupAll", function(data) {
		if (data.msg == "success") {
			var html = '';
			$.each(data.datas,function(k,v){
				html += '<option value="'+v.id+'" >'+v.name+'</option>';
			});
			$("#taskGroupName").append(html);
			$("#taskGroupName").val(groupId);
			$("#addGroupName").append(html);
		}
	});
}

function addOrEdit(){
	$("#id").val("");
	$("#addTaskName").val("");
	$("#addTaskSummary").val("");
	$("#addGroupName").val($("#taskGroupName").val());
	var o,title="新增采集任务";
	if(arguments.length != 0){
		o = arguments[0];
		title = "编辑采集任务";
	}
	layer.open({
		title:title,
		type: 1,
		area : [ '600px', '510px' ],
		content: $('#add'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
		btn: ['保存', '关闭'],
		yes: function(index){
			var taskName = $.trim($("#addTaskName").val());
			if(taskName == ''){
				layer.msg('请输入任务名称');
				return;
			}
			var taskGroupId = $("#addGroupName").val();
			if(taskGroupId == 0){
				layer.msg('请选择所属任务组');
				return;
			}
			var task = {};
			var id = $("#id").val();
			if(id != ""){
				task["id"] = id;
			}
			task["name"] = taskName;
			task["summary"] = $("#addTaskSummary").val();
			task["taskGroupId"] = taskGroupId;
			$.post("isTaskExist",{id:id,taskGroupId:taskGroupId,name:taskName}, function(data) {
				if(!data.msg){
					$.post("saveOrUpdateTask",task, function(data2) {
						if(!data2.msg){
							layer.msg("保存失败");
						}else{
							layer.msg("保存成功");
							layer.close(index);
							search(1);
						}
					});
				}else{
					layer.msg("任务名称已存在");
				}
			});
			
		},
		close: function(){
			
		},
		success:function(){
			if(o){
				var parent = $(o).parent(); 
				$("#id").val(parent.attr("id"));
				$("#addTaskName").val(parent.attr("name"));
				$("#addTaskSummary").val(parent.attr("summary"));
				$("#addGroupName").val(parent.attr("groupId"));
			}
		}
	});
}

function toConfigHtml(taskId){
	window.location.href = "spider-task-config-manage.html?taskId="+taskId;
}